package com.allqj.sfx.domain.utils;

import com.allqj.sfx.domain.entity.UserInfo;
import com.allqj.sfx.infrastructure.enums.ResultStatusCodeEnum;
import com.allqj.sfx.infrastructure.exceptions.BusinessException;

/**
 * @author sjw
 * @className UserContextUtil
 * @date 2022/7/20 17:49
 * @description: 用户上下文工具
 */
public class UserContextHolder {
    private static ThreadLocal<UserInfo> contextHolder = new ThreadLocal<UserInfo>();

    public static void setUserContext(UserInfo userInfo) {
        contextHolder.set(userInfo);
    }

    public static UserInfo getUserContext() {
        UserInfo userInfo = contextHolder.get();
        if (userInfo == null)
            throw new BusinessException(ResultStatusCodeEnum.NOT_LOGIN.getCode(), ResultStatusCodeEnum.NOT_LOGIN.getMessage());
        return userInfo;
    }
}
